/**
 * 
 */
package de.oszimtcc.framework;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import de.oszimtcc.timetable.R;
import de.oszimtcc.timetable.TimetableApplication;
import de.oszimtcc.timetablehelper.RequestCache;
import de.oszimtcc.timetablelistener.HideAfterEndAnimationListener;
import de.oszimtcc.timetablelistener.ShowAfterEndAnimationListener;
import de.oszimtcc.timetablescreens.ConfigurationScreen;
import de.oszimtcc.timetablescreens.TimetableScreen;
import de.oszimtcc.timetableservices.TimetableServiceConnection;
import de.oszimtcc.timetableservices.TimetableChangingService;

/**
 * @author Kooki
 *
 */
public class BaseActivity extends Activity
{
	public BaseScreen screen;
	public LayoutInflater inflater;
	private TimetableServiceConnection serviceConnection;
	private View rootView;
	private float scale = 1.0f;
	private int displayWidth;
	private int displayHeight;
	private MediaPlayer mp;
	private String activityStatus = "";
	
	public BaseActivity() 
	{
		super();
		TimetableApplication.getInstance().setActivity(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		SetActivityStatus("onCreate");
		SetStringPreference("flipperInitPosition", ""+(Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 2));
		inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		
		if(savedInstanceState!=null && savedInstanceState.containsKey("lastscreen")) 
		{
			screen = BaseScreen.InstantiateScreen(this, savedInstanceState.getString("lastscreen"));
		} 
		else 
		{ 
			if(GetStringPreference("Class") != "")
				screen =  new TimetableScreen(this);
			else
				screen = new ConfigurationScreen(this);
		}
		scale = getResources().getDisplayMetrics().density;
		displayWidth = getResources().getDisplayMetrics().widthPixels;
		displayHeight = getResources().getDisplayMetrics().heightPixels;
		//serviceConnection = new TimetableServiceConnection(this);
		//serviceConnection.bindService(TimetableChangingService.class);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) 
	{
		outState.putString("lastscreen", screen.getClass().getName());
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onResume() 
	{
		SetActivityStatus("onResume");
		super.onResume();

		if(screen ==null)
			screen =  new TimetableScreen(this);
	}

	@Override
	protected void onPause() 
	{
		SetActivityStatus("onPause");
		if (mp!=null)
			mp.pause();
		super.onPause();
	}

	@Override
	protected void onDestroy() 
	{
		SetActivityStatus("onDestroy");
		screen = null;		
		mp = null;
		RequestCache.Close();
		super.onDestroy();
	}

	@Override
	protected void onStop() 
	{
		SetActivityStatus("onStop");
		RequestCache.Close();
		super.onStop();
	}
	
	@Override
	protected void onRestart() 
	{
		SetActivityStatus("onRestart");
		super.onRestart();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		switch (keyCode) 
		{
			case KeyEvent.KEYCODE_BACK: 
			{
					if(Back()) 
						return true;
			}		
		}
		return super.onKeyDown(keyCode, event);
	}

	public boolean onCreateOptionsMenu(Menu menu)
    {
    	getMenuInflater().inflate(R.menu.main_menu, menu);
    	return super.onCreateOptionsMenu(menu);
    }
	
	public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch (item.getItemId())
    	{
	    	case R.id.configuration:
	    		screen = this.screen.InstantiateScreen(ConfigurationScreen.class, screen.getClass());
	    		return true;
	    	case R.id.about:
	    		return true;
	
			default:
				return super.onOptionsItemSelected(item);
    	}
    }
	
	private boolean Back() 
	{
		return screen.Back();
	}

	@Override
	public void setContentView(int layoutResID) 
	{
		rootView = inflater.inflate(layoutResID, null);
		if(rootView!=null) 
		{
			setContentView(rootView);
		}
	}

	public float getScale() 
	{
		return scale;
	}

	public int getDisplayHeight() 
	{
		return displayHeight;
	}

	public int getDisplayWidth() 
	{
		return displayWidth;
	}

	public void showUrl(String webUrl) 
	{
		Uri uri = Uri.parse( webUrl );  
		Intent it = new Intent(Intent.ACTION_VIEW, uri);  
		startActivity(it);	
	}

	public void hideView(int id) 
	{
		View v = findViewById(id);
		if(v!=null) 
		{ 
			v.setVisibility(View.INVISIBLE);
		}
	}

	public void showView(int id) 
	{
		View v = findViewById(id);
		if(v!=null) 
		{ 
			v.setVisibility(View.VISIBLE);
		}
	}

	public void startRootAnimationThenShow(int animId) 
	{
		if(rootView!=null)
			startAnimationThenShow(rootView.getId(), animId);
	}

	public Animation startAnimation(int viewId, int animId) 
	{
		return startAnimation(findViewById(viewId), animId);
	}

	protected Animation startAnimation(View view, int animId) 
	{
		Animation a =  AnimationUtils.loadAnimation(this, animId);
		view.startAnimation(a);
		return a;
	}

	public Animation startRootAnimation(int animId) 
	{
		return startAnimation(rootView,animId);
	}

	public void startAnimationThenShow(int viewId, int animId) 
	{
		startAnimation(viewId,animId).setAnimationListener(new ShowAfterEndAnimationListener(findViewById(viewId)));
	}

	public void startAnimationThenHide(View viewToAnimate, int animId) 
	{
		startAnimation(viewToAnimate,animId).setAnimationListener(new HideAfterEndAnimationListener(viewToAnimate));
	}
	
	public void startAnimationThenHide(int viewId, int animId) 
	{
		startAnimation(viewId,animId).setAnimationListener(new HideAfterEndAnimationListener(findViewById(viewId)));
	}
	
	public void startAnimationThenHide(View vToHideOn,int viewIdToHide , int animId) 
	{
		View toAnimate = vToHideOn.findViewById(viewIdToHide);
		startAnimation(toAnimate,animId).setAnimationListener(new HideAfterEndAnimationListener(toAnimate.findViewById(viewIdToHide)));
	}

	public void startSound(int resid)
	{
		mp = MediaPlayer.create(this, resid);
		mp.start();		
	}
	
	public void loopSound(int resid) 
	{
		mp = MediaPlayer.create(this, resid);
		mp.setLooping(true);
		mp.start();
	}
	
	public void stopSound(int resid) 
	{
		if (mp.isPlaying())
			mp.stop();	
		mp = null;
	}
	
	public void SetActivityStatus(String pNewStatus)
	{
		Logger.Info("Activittystatus changed to :", pNewStatus);
		this.activityStatus = pNewStatus;
	}
	
	public String GetActivityStatus()
	{
		return this.activityStatus;
	}
	
	public void SetStringPreference(String pKey, String pToSet)
	{
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
		pref.edit().putString(pKey, pToSet).commit();
	}
	
	public String GetStringPreference(String pKey)
	{
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
		return pref.getString(pKey,"");
	}
	
	public void SetBooleanPreference(String pKey, Boolean pToSet)
	{
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
		pref.edit().putBoolean(pKey, pToSet).commit();
	}
	
	public Boolean GetBooleanPreference(String pKey)
	{
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
		return pref.getBoolean(pKey,false);
	}
	
	public void SetIntPreference(String pKey, int pToSet)
	{
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
		pref.edit().putInt(pKey, pToSet).commit();
	}
	
	public int GetIntegerPreference(String pKey)
	{
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
		return pref.getInt(pKey,0);
	}
	
	public View getRootView() 
	{
		return rootView;
	}
	
	public TimetableServiceConnection getServiceConnection() 
	{
		return serviceConnection;
	}
}
